{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "277755c8",
   "metadata": {},
   "source": [
    "# 文件读写\n",
    "\n",
    "写入测试文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3876480d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing test.txt\n"
     ]
    }
   ],
   "source": [
    "%%writefile test.txt\n",
    "this is a test file.\n",
    "hello world!\n",
    "python is good!\n",
    "today is a good day.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ce5eba",
   "metadata": {},
   "source": [
    "## 读文件\n",
    "\n",
    "使用 `open()` 函数来读文件，使用文件名的字符串作为输入参数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "be084a5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('test.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "077593e8",
   "metadata": {},
   "source": [
    "可以使用 `.read()`方法来读入文件中的所有内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9f58ab57",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6469b91b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'this is a test file.\\nhello world!\\npython is good!\\ntoday is a good day.\\n'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e5d76d8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is a test file.\n",
      "hello world!\n",
      "python is good!\n",
      "today is a good day.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed3ade9a",
   "metadata": {},
   "source": [
    "使用完文件之后，需要将文件关闭："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ad814157",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e90ea33f",
   "metadata": {},
   "source": [
    "也可以用`.readlines()`方法按照行读入内容。该方法返回一个列表，每个元素代表文件中每一行的内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "893854d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['this is a test file.\\n', 'hello world!\\n', 'python is good!\\n', 'today is a good day.\\n']\n"
     ]
    }
   ],
   "source": [
    "f = open('test.txt')\n",
    "\n",
    "lines = f.readlines()\n",
    "\n",
    "f.close()\n",
    "\n",
    "print(lines)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b855f8f",
   "metadata": {},
   "source": [
    "还可以将f放入一个循环中，遍历："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1c4c8e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is a test file.\n",
      "\n",
      "hello world!\n",
      "\n",
      "python is good!\n",
      "\n",
      "today is a good day.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "f = open('test.txt')\n",
    "for line in f:\n",
    "    print(line)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16e53680",
   "metadata": {},
   "source": [
    "删除演示文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3cbe2ad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rm test.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a41c3e7",
   "metadata": {},
   "source": [
    "## 写文件\n",
    "\n",
    "写文件也使用`open()`函数，只需要额外指定模式参数为`w`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "39096110",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('myfile.txt', 'w')\n",
    "f.write('hello world!')\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f578e48",
   "metadata": {},
   "source": [
    "使用 `w` 模式时，如果文件不存在会被创建："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f1f9d944",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(open('myfile.txt').read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cde50f30",
   "metadata": {},
   "source": [
    "如果文件存在，原先的内容会被完全覆盖："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cb0cbe88",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('myfile.txt', 'w')\n",
    "f.write('another hello world!')\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "112b090a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "another hello world!\n"
     ]
    }
   ],
   "source": [
    "print(open('myfile.txt').read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33f14762",
   "metadata": {},
   "source": [
    "如果不想覆盖，则需要使用追加模式`a`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "71055e88",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('myfile.txt', 'a')\n",
    "f.write('... and more')\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ce5613ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "another hello world!... and more\n"
     ]
    }
   ],
   "source": [
    "print(open('myfile.txt').read())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a83e6cae",
   "metadata": {},
   "source": [
    "删除演示文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1ffaac0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "%rm myfile.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "00fde111",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
